package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscNetInfoResource;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItDict;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItDictData;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscNetInfoDto;
import com.glsc.ngateway.platform.service.glres.GlscNetInfoResourceService;
import com.glsc.ngateway.platform.service.it.DictionaryService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 资源管理-网络资源管理
 */
@RestController
@RequestMapping("/api/res/netInfo")
@Slf4j
public class GlscNetInfoResourceController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscNetInfoResourceService netInfoResourceService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscNetInfoResource>> page(@RequestBody GlscNetInfoDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }

        if (StringUtils.isNotBlank(dto.getAddress())) {
            param.put("LIKE_address", dto.getAddress());
        }

        /*ItDictData dictData = dictionaryService.findByDictData("t_res_sys_all_auth_manage");
        List<ItDict> dictValues = dictData.getDictValues();
        Set<String> superOaAccounts = dictValues.stream().map(ItDict::getDictSubentry).collect(Collectors.toSet());
        PlatformUserDto user = userCache.get(request);
        if (!superOaAccounts.contains(user.getUsername())) {
            param.put("EQ_userId", user.getId());
        }*/

        Page<GlscNetInfoResource> page = netInfoResourceService.page(dto.getPageNo(), dto.getPageSize(), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<GlscNetInfoResource> findById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(netInfoResourceService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody GlscNetInfoResource obj, HttpServletRequest request) {
        PlatformUserDto user = userCache.get(request);
        obj.setUserId(user.getId());
        obj.setUserName(user.getName());
        obj.setUserOaAccount(user.getUsername());
        netInfoResourceService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据地删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody GlscNetInfoResource obj) {
        netInfoResourceService.delete(obj.getId());
        return PlatformResponse.success();
    }


}
